package com.cs.archivesmanagement.serviceimpl;

import com.cs.archivesmanagement.dao.EntityDao;
import com.cs.archivesmanagement.entity.AmAccount;
import com.cs.archivesmanagement.entity.AmDepartment;
import com.cs.archivesmanagement.entity.AmRole;
import com.cs.archivesmanagement.service.AccountService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.ArrayList;

/**
 * Created by amo on 2015/8/17.
 */

@Repository
public class AccountServiceImpl implements AccountService {


    @Resource
    EntityDao entityDao;

    @Override
    public AmAccount findById(Integer id) {

        return (AmAccount)entityDao.getEntity(AmAccount.class,id);
    }

    @Override
    public boolean regist(AmAccount account) {

        ArrayList<Criterion> criterions=new ArrayList<>();

        criterions.add(Restrictions.eq("accountAc", account.getAccountAc()));

        ArrayList<AmAccount> accountArrayList=(ArrayList<AmAccount>)entityDao.getEntityList(AmAccount.class,criterions);

        return  accountArrayList.size()==0&&entityDao.addEntity(account)!=null;

    }


    @Override
    public ArrayList<AmAccount> getAllAccountByDepartmend(Integer departmentId) {

        ArrayList<Criterion> criterions=new ArrayList<>();

        AmDepartment amDepartment=new AmDepartment();

        amDepartment.setDepartmentId(departmentId);

        criterions.add(Restrictions.eq("accountDepartment",amDepartment ));

        ArrayList<AmAccount> accountArrayList=(ArrayList<AmAccount>)entityDao.getEntityList(AmAccount.class,criterions);

        return  accountArrayList;
    }

    @Override
    public boolean deleteAccount(AmAccount amAccount) {
       return  entityDao.deleteEntity(amAccount);
    }


    @Override
    public AmAccount login(String accountName, String psw) {

        ArrayList<Criterion> criterions=new ArrayList<>();

        criterions.add(Restrictions.eq("accountAc", accountName));

        ArrayList<AmAccount> accountArrayList=(ArrayList<AmAccount>)entityDao.getEntityList(AmAccount.class,criterions);

        if(accountArrayList.size()==0) return null;

        AmAccount amAccount=accountArrayList.get(0);

        if(amAccount.getAccountPsw().equals(psw))return amAccount;

        else return null;

    }


    @Override
    public ArrayList<AmAccount> getAllAccount() {


        return (ArrayList<AmAccount>)entityDao.getEntityList(AmAccount.class,null);
    }

    @Override
    public boolean addAmRole(AmRole amRole) {
       return  entityDao.addEntity(amRole)!=null;
    }

    @Override
    public boolean upadateAmRole(AmRole amRole) {
        return entityDao.updateEntity(amRole);
    }

    @Override
    public boolean deleteAmRole(AmRole amRole) {
        return entityDao.deleteEntity(amRole);
    }

    @Override
    public ArrayList<AmRole> getAllAmRole() {


        return  ( ArrayList<AmRole>)entityDao.getEntityList(AmRole.class,null);
    }

    @Override
    public boolean addAmDepartment(AmDepartment amDepartment) {
        return entityDao.addEntity(amDepartment)!=null;
    }

    @Override
    public boolean upadateAmDepartment(AmDepartment amDepartment) {
        return entityDao.updateEntity(amDepartment);
    }

    @Override
    public boolean deleteAmDepartment(AmDepartment amDepartment) {
        return entityDao.deleteEntity(amDepartment);
    }

    @Override
    public ArrayList<AmDepartment> getAllAmDepartment() {

        return  ( ArrayList<AmDepartment>)entityDao.getEntityList(AmDepartment.class,null);
    }



}
